//
//  LoginController.m
//  projectBase
//
//  Created by zhuangzhh on 15/5/8.
//  Copyright (c) 2015年 zhuangzhh. All rights reserved.
//

#import "LoginController.h"
#import "HttpBaseModel.h"
#import "DSToast.h"
#import "PreviewImageView.h"
#import <UShareUI/UShareUI.h>
#import <UMSocialCore/UMSocialCore.h>
#import <UIKit/UIKit.h>


#define RegisterBtnTag   20000
#define LoginBtnTag      20001
#define AutoLoginBtnTag  20002
#define ForgetBtnTag     20003
#define SinaBtnTag       20004
#define WeixinBtnTag     20005
#define QQBtnTag         20006
#define LineWidth        .7

@interface LoginController ()

@end

@implementation LoginController

-(void)login:(NSString *)name pwd:(NSString *)pwd fromtype:(NSString *)fromtype nickname:(NSString *)nickname
{
    [self showLoadViewExt:CGRectMake(0, 0, ScreenWidth, ScreenHeight-TopAndSystemHeight) backColor:[UIColor clearColor] info:@"正在加载中，请稍候..."];
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithObjectsAndKeys:name,@"username",pwd,@"password",nil];
//    [param setObject:[AppData getpersonshopmid] forKey:@"mid"];
    [[HttpBaseModel getInstance] getDataResponsePath:[NSString stringWithFormat:@"user/userLogin"] params:param httpMethod:@"POST" onCompletion:^(NSData *responseData){
        
        [self closeLoadView];
        NSError *error = nil;
        id resultString = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
        if (error)
        {
            NSLog(@"dic->%@",error);
        }
        else
        {
            ResponseCell *response = [[ResponseCell alloc] initWithDictionary:resultString error:NULL];
            if(response.success == 1)
            {
                [[NSUserDefaults standardUserDefaults] setObject:_pwdText.text forKey:@"pwd"];
                [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"isthird"];
//                if(name == nil)
//                {
//                    if([[NSUserDefaults standardUserDefaults] objectForKey:@"autologin"]!=nil)
//                    {
//                        [[NSUserDefaults standardUserDefaults] setObject:_phoneText.text forKey:@"user"];
//                    }
//                }
//                else
//                {
//                    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"autologin"];
//                }

                NSArray *array= [resultString objectForKey:@"data"];
                if([array isKindOfClass:[NSNull class]])
                    return ;
                if([array count]>0)
                {
                    NSDictionary *dic = [array objectAtIndex:0];
                    [[NSUserDefaults standardUserDefaults]setObject:[dic objectForKey:@"user_id"] forKey:@"uid"];
                    [[NSUserDefaults standardUserDefaults]setObject:[dic objectForKey:@"username"] forKey:@"username"];
                    
                    [AppData shareInstance].personCell = [[PersonCell alloc] initWithDictionary:dic error:NULL];
                    [self receivingAddress:[AppData shareInstance].personCell.user_id];
                    [AppData shareInstance].isLogin = YES;
                    [[ViewManager shareInstance].NavigationController popViewControllerAnimated:YES];
                    [[NSNotificationCenter defaultCenter] postNotificationName:Notify_TabBarOfIndex
                                                                        object:[NSNumber numberWithInt:_type]];
                }
                [[AppData shareInstance].loginmidarray removeAllObjects];
                NSArray *midarray = [resultString objectForKey:@"mid"];
                for(int index = 0;index < [midarray count];index++)
                {
                    NSDictionary *dic = [midarray objectAtIndex:index];
                    [[AppData shareInstance].loginmidarray addObject:dic];
                    if(index == 0)
                    {
                        NSString *mid = [dic objectForKey:@"mid"];
                        [AppData setpersonshopmid:mid];
                    }
                }

            }
            else
            {
                DSToast *toast = [[DSToast alloc]initWithText:response.msg];
                [toast showInView:self.view];
//                [self showTipInfo:response.msg];
            }
            
        }
        
        
        
    } onError:^(NSError *error){
        DSToast *toast = [[DSToast alloc]initWithText:@"网络异常，请重试！"];
        [toast showInView:self.view];
        [self closeLoadView];
    }];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSString *phoneNum = [[NSUserDefaults standardUserDefaults]objectForKey:@"userPhoneNum"];
    NSString *pwd = [[NSUserDefaults standardUserDefaults]objectForKey:@"userPassword"];
    if (![PubFunction stringIsNullOrEmpty:phoneNum] || ![PubFunction stringIsNullOrEmpty:pwd])
    {
        [self login:phoneNum pwd:pwd fromtype:@"1" nickname:nil];
    }
}

-(void) initUI
{
    [super initUI];
    [self setTopTitle:@"登录"];
    [self setTopTitleColor:[UIColor whiteColor]];
    [self setTopBgColor:MainColor];
    self.view.backgroundColor = UIColorRGB(245);
    
    UIButton *registerBtn = [self createBtn:CGRectMake(ScreenWidth-60, TopBarHeight-34, 60, 30) title:@"注册" iconImage:nil backgroundImage:nil tag:RegisterBtnTag textColor:[UIColor whiteColor]];
    registerBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [topBarView addSubview:registerBtn];
    
    UIView *inputView = [self createInputView:CGRectMake(0, TopBarHeight+30, ScreenWidth, 80)];
    [self.view addSubview:inputView];
    
    UIButton *loginBtn = [self createBtn:CGRectMake(13, inputView.frame.size.height+inputView.frame.origin.y+20, ScreenWidth-26, 36) title:@"登录" iconImage:nil backgroundImage:nil tag:LoginBtnTag textColor:[UIColor whiteColor]];
    loginBtn.backgroundColor = MainColor;
    loginBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    loginBtn.layer.cornerRadius = 3.0;
    [self.view addSubview:loginBtn];
    
    UIImage *icon = nil;
    NSString *isLogin = [[NSUserDefaults standardUserDefaults] objectForKey:@"autologin"];
    if(isLogin == nil)
    {
        icon =[PubFunction GetSkinImage:@"normal_image"];
    }
    else
        icon =[PubFunction GetSkinImage:@"select_image"];

    UIButton *autoLoginBtn = [self createBtn:CGRectMake(loginBtn.frame.origin.x, loginBtn.frame.size.height+loginBtn.frame.origin.y+5, 80, 20) title:@" 自动登录" iconImage:icon backgroundImage:nil tag:AutoLoginBtnTag textColor:UIColorRGB(137)];
    autoLoginBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:autoLoginBtn];
    
    UIButton *forgetBtn = [self createBtn:CGRectMake(loginBtn.frame.origin.x+loginBtn.frame.size.width-60, loginBtn.frame.size.height+loginBtn.frame.origin.y+5, 50, 20) title:@"忘记密码" iconImage:nil backgroundImage:nil tag:ForgetBtnTag textColor:UIColorRGB(137)];
    forgetBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:forgetBtn];
    
    UIView *leftLine = [[UIView alloc] initWithFrame:CGRectMake(13, ScreenHeight-((ScreenWidth-40)/3.0)-20, (ScreenWidth-26-90)/2.0, LineWidth)];
    leftLine.backgroundColor = UIColorRGB(179);
    leftLine.alpha = .4;
    [self.view addSubview:leftLine];
    
    UILabel *tipLab = [[UILabel alloc] initWithFrame:CGRectMake(leftLine.frame.size.width+leftLine.frame.origin.x, leftLine.frame.origin.y-7, 90, 14)];
    tipLab.font = [UIFont systemFontOfSize:13];
    tipLab.text = @"第三方登录";
    tipLab.textColor = UIColorRGB(61);
    tipLab.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:tipLab];
    
    UIView *rightLine = [[UIView alloc] initWithFrame:CGRectMake(tipLab.frame.size.width+tipLab.frame.origin.x, ScreenHeight-((ScreenWidth-40)/3.0)-20, (ScreenWidth-100)/2.0-8, LineWidth)];
    rightLine.backgroundColor = UIColorRGB(179);
    rightLine.alpha = .4;
    [self.view addSubview:rightLine];
    
    double btnwidth = (ScreenWidth-40)/3.0;
    UIButton *sinaBtn = [self createBtn:CGRectMake(20, rightLine.frame.origin.y, btnwidth, btnwidth) title:nil iconImage:[PubFunction GetSkinImage:@"sina_icon"] backgroundImage:nil tag:SinaBtnTag textColor:nil];
    sinaBtn.hidden = YES;
    [self.view addSubview:sinaBtn];
    
    UIButton *weixinBtn = [self createBtn:CGRectMake(ScreenWidth/2-20-btnwidth, rightLine.frame.origin.y, btnwidth, btnwidth) title:@"微信登陆" iconImage:nil backgroundImage:nil tag:WeixinBtnTag textColor:[UIColor blackColor]];
    weixinBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:weixinBtn];
    
    UIButton *qqBtn = [self createBtn:CGRectMake(ScreenWidth/2+20, rightLine.frame.origin.y, btnwidth, btnwidth) title:@"分享" iconImage:nil backgroundImage:nil tag:QQBtnTag textColor:[UIColor blackColor]];
    qqBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:qqBtn];
    
}

-(UIView *)createInputView:(CGRect)frame
{
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.backgroundColor = [UIColor whiteColor];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, LineWidth)];
    line.backgroundColor = UIColorRGB(217);
    line.alpha = .6;
    [view addSubview:line];
    
    _phoneText = [[UITextField alloc] initWithFrame:CGRectMake(13,0,ScreenWidth-26, frame.size.height/2.0)];
    _phoneText.font = [UIFont systemFontOfSize:14];
    _phoneText.placeholder = @"手机号";
    [view addSubview:_phoneText];
    
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(13, frame.size.height/2.0, ScreenWidth-26, LineWidth)];
    line1.backgroundColor = UIColorRGB(217);
    line1.alpha = .6;
    [view addSubview:line1];
    
    _pwdText = [[UITextField alloc] initWithFrame:CGRectMake(13,line1.frame.origin.y,_phoneText.frame.size.width-26, frame.size.height/2.0)];
    _pwdText.secureTextEntry = YES;
    _pwdText.font = [UIFont systemFontOfSize:14];
    _pwdText.placeholder = @"密码";
    [view addSubview:_pwdText];
    
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(0,frame.size.height-LineWidth, ScreenWidth, LineWidth)];
    line2.backgroundColor = UIColorRGB(217);
    line2.alpha = .6;
    [view addSubview:line2];
    
    return view;
    
    
}

-(void)btnAction:(id)sender
{
    UIButton *btn  = (UIButton *)sender;
    NSInteger tag = [btn tag];
    if(tag == AutoLoginBtnTag)
    {
        if([[NSUserDefaults standardUserDefaults] objectForKey:@"autologin"]==nil)
        {
            //自动登录
            [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"autologin"];
            [btn setImage:[PubFunction GetSkinImage:@"select_image"] forState:UIControlStateNormal];
        }
        else
        {
            //取消自动登录
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"autologin"];
            [btn setImage:[PubFunction GetSkinImage:@"normal_image"] forState:UIControlStateNormal];
        }
    

    }
    else if(tag == RegisterBtnTag)
    {
        RegisterController *vc= [[RegisterController alloc] init];
        vc.barTitle = @"注册";
        [[ViewManager shareInstance].NavigationController pushViewController:vc animated:YES];
    }
    else if (tag == ForgetBtnTag)
    {
//        ForgetController *vc= [[ForgetController alloc] init];
//        [[ViewManager shareInstance].NavigationController pushViewController:vc animated:YES];
    }
    else if (tag == LoginBtnTag)
    {
//        _phoneText.text = @"17750207606";
//        _pwdText.text = @"123";
        [_phoneText resignFirstResponder];
        [_pwdText resignFirstResponder];
        if([PubFunction stringIsNullOrEmpty:_phoneText.text]||_phoneText.text == nil)
        {
            DSToast *toast = [[DSToast alloc]initWithText:@"用户名不能为空！"];
            [toast showInView:self.view];
//            [self showTipInfo:@"用户名不能为空！"];
            return;
        }
        if([PubFunction stringIsNullOrEmpty:_pwdText.text]||_pwdText.text == nil)
        {
            DSToast *toast = [[DSToast alloc]initWithText:@"密码不能为空！"];
            [toast showInView:self.view];
//            [self showTipInfo:@"密码不能为空！"];
            return;
        }
        
        //自动登录
        if([[NSUserDefaults standardUserDefaults] objectForKey:@"autologin"]!= nil)
        {
            [[NSUserDefaults standardUserDefaults] setObject:_phoneText.text forKey:@"userPhoneNum"];
            [[NSUserDefaults standardUserDefaults] setObject:_pwdText.text forKey:@"userPassword"];
        }

        
        [self login:_phoneText.text pwd:_pwdText.text fromtype:@"0" nickname:nil];
//        [AppData shareInstance].isLogin = YES;
//        [[ViewManager shareInstance].NavigationController popViewControllerAnimated:YES];
    }
    //微信登陆
    else if(tag == WeixinBtnTag)
    {
        [UMSocialUIManager setPreDefinePlatforms:@[@(UMSocialPlatformType_QQ),@(UMSocialPlatformType_WechatSession),@(UMSocialPlatformType_Sina),@(UMSocialPlatformType_Qzone),@(UMSocialPlatformType_WechatTimeLine)]];
        [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
            // 根据获取的platformType确定所选平台进行下一步操作
            [self getUserInfoForPlatform:platformType];
            
        }];
    }
    //分享
    else if(tag == QQBtnTag)
    {
        [UMSocialUIManager setPreDefinePlatforms:@[@(UMSocialPlatformType_QQ),@(UMSocialPlatformType_WechatSession),@(UMSocialPlatformType_Sina),@(UMSocialPlatformType_Qzone),@(UMSocialPlatformType_WechatTimeLine)]];
        [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
            // 根据获取的platformType确定所选平台进行下一步操作
            [self shareTextToPlatformType:platformType];

        }];
    }
    else if(tag == SinaBtnTag)
    {
        /*
        NSString *sharePlat = nil;
        if(tag == QQBtnTag)
        {
            sharePlat = UMShareToQQ;
            //[UMSocialData setAppKey:@"58cd0c5b8f4a9d0e840006a4"];
        }
        else if(tag == WeixinBtnTag)
        {
            sharePlat = UMShareToWechatSession;
            //[UMSocialData setAppKey:@"563c4af467e58e523600246e"];
        }
        else
            sharePlat = UMShareToSina;
        
        [UMSocialControllerService defaultControllerService].socialUIDelegate = self;
        UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:sharePlat];
        snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
            NSLog(@"login response is %@",response);
            //          获取微信用户名、uid、token等
            if (response.responseCode == UMSResponseCodeSuccess) {
                UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:sharePlat];
                NSLog(@"username is %@, uid is %@, token is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken);
                if(snsAccount == nil)
                    return ;
                else
                {
                    
                    [self thirdPartyLogin:snsAccount.usid nick_name:snsAccount.userName head_url:snsAccount.iconURL];
                    
                    //                [[NSUserDefaults standardUserDefaults] setObject:snsAccount.usid forKey:@"wxusid"];
                    //                [self login:snsAccount.usid nick_name:snsAccount.userName headurl:snsAccount.iconURL];
                }
                
            }
        });

        
//        [UMSocialControllerService defaultControllerService].socialUIDelegate = self;
//        UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];
//        snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
//            //  获取微博用户名、uid、token等
//            if (response.responseCode == UMSResponseCodeSuccess) {
//                UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary]valueForKey:UMShareToWechatSession];
//                NSLog(@"username is %@, uid is %@, token is %@ iconUrl is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
//            }
//        });
        
        
        // 第三遍
//        [UMSocialControllerService defaultControllerService].socialUIDelegate = self;
//        [UMSocialData setAppKey:@"58bd49e27666131f8e001146"];
//        [UMSocialWechatHandler setWXAppId:@"wxa5c6354c09fa7840" appSecret:@"083cfff84bb08f31eadc97bef9708029" url:@"http://www.umeng.com/social"];
//
//        UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];
//        snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
//            //  获取微博用户名、uid、token等
//            if (response.responseCode == UMSResponseCodeSuccess) {
//                UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary]valueForKey:UMShareToWechatSession];
//                NSLog(@"username is %@, uid is %@, token is %@ iconUrl is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
//            }
//        });

        
//        UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:sharePlat];
//        snsPlatform.needLogin = YES;
//        
//        snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
//            NSLog(@"login response is %@",response);
//            //          获取微博用户名、uid、token等
//            if (response.responseCode == UMSResponseCodeSuccess) {
//                UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:sharePlat];
//                NSLog(@"username is %@, uid is %@, token is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken);
//                if([[NSUserDefaults standardUserDefaults] objectForKey:@"autologin"]!= nil)
//                {
//                    [[NSUserDefaults standardUserDefaults] setObject:snsAccount.userName forKey:@"username"];
//                    [[NSUserDefaults standardUserDefaults] setObject:snsAccount.usid forKey:@"usid"];
//                    [[NSUserDefaults standardUserDefaults] setObject:snsAccount.iconURL forKey:@"iconurl"];
//
//                }
//                [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"isthird"];
//                [self thirdPartyLogin:snsAccount.usid nick_name:snsAccount.userName head_url:snsAccount.iconURL];
//            }
//        });
         */
    }
}
//分享
- (void)shareTextToPlatformType:(UMSocialPlatformType)platformType
{
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    //设置文本
    messageObject.text = @"社会化组件UShare将各大社交平台接入您的应用，快速武装App。";
    
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            NSLog(@"************Share fail with error %@*********",error);
        }else{
            NSLog(@"response data is %@",data);
        }
    }];
}

//登陆
- (void)getUserInfoForPlatform:(UMSocialPlatformType)platformType
{
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:platformType currentViewController:self completion:^(id result, NSError *error) {
        
        UMSocialUserInfoResponse *resp = result;
        
        // 第三方登录数据(为空表示平台未提供)
        // 授权数据
        NSLog(@" uid: %@", resp.uid);
        NSLog(@" openid: %@", resp.openid);
        NSLog(@" accessToken: %@", resp.accessToken);
        NSLog(@" refreshToken: %@", resp.refreshToken);
        NSLog(@" expiration: %@", resp.expiration);
        
        // 用户数据
        NSLog(@" name: %@", resp.name);
        NSLog(@" iconurl: %@", resp.iconurl);
        NSLog(@" gender: %@", resp.unionGender);
        
        // 第三方平台SDK原始数据
        NSLog(@" originalResponse: %@", resp.originalResponse);
    }];
}


-(void)thirdPartyLogin:(NSString *)uid nick_name:(NSString *)nick_name head_url:(NSString *)head_url
{
    [self showLoadViewExt:CGRectMake(0, 0, ScreenWidth, ScreenHeight-TopAndSystemHeight) backColor:[UIColor clearColor] info:@"正在加载中，请稍候..."];
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithObjectsAndKeys:uid,@"uid",head_url,@"head_url",nick_name,@"nick_name",[AppData getpersonshopmid],@"mid",nil];
    [[HttpBaseModel getInstance] getDataResponsePath:[NSString stringWithFormat:@"user/thirdPartyLogin"] params:param httpMethod:@"POST" onCompletion:^(NSData *responseData){
        
        [self closeLoadView];
        NSError *error = nil;
        id resultString = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
        if (error)
        {
            NSLog(@"dic->%@",error);
        }
        else
        {
            ResponseCell *response = [[ResponseCell alloc] initWithDictionary:resultString error:NULL];
            if(response.success == 1)
            {
                [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"isthird"];
                NSArray *array= [resultString objectForKey:@"data"];
                if([array isKindOfClass:[NSNull class]])
                    return ;
                if([array count]>0)
                {
                    NSDictionary *dic = [array objectAtIndex:0];
                    [[NSUserDefaults standardUserDefaults]setObject:[dic objectForKey:@"user_id"] forKey:@"uid"];
                    [[NSUserDefaults standardUserDefaults]setObject:[dic objectForKey:@"username"] forKey:@"username"];
                    [[NSUserDefaults standardUserDefaults]setObject:[dic objectForKey:@"alias"] forKey:@"third_alias"];
                    [[NSUserDefaults standardUserDefaults]setObject:[dic objectForKey:@"create_time"] forKey:@"third_create_time"];
                    [[NSUserDefaults standardUserDefaults]setObject:[dic objectForKey:@"user_img"] forKey:@"third_userimg"];
                    NSLog(@"%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"alias"]);
                    [AppData shareInstance].personCell = [[PersonCell alloc] initWithDictionary:dic error:NULL];
                    [self receivingAddress:[AppData shareInstance].personCell.user_id];
                    [AppData shareInstance].isLogin = YES;
                    [[ViewManager shareInstance].NavigationController popViewControllerAnimated:YES];
//                    if(_type == 0)
//                        return;
                    [[NSNotificationCenter defaultCenter] postNotificationName:Notify_TabBarOfIndex
                                                                        object:[NSNumber numberWithInt:_type]];
                    [[ViewManager shareInstance].NavigationController popViewControllerAnimated:YES];
                }
                
                NSArray *midarray = [resultString objectForKey:@"mid"];
                for(int index = 0;index < [midarray count];index++)
                {
                    NSDictionary *dic = [midarray objectAtIndex:index];
                    [[AppData shareInstance].loginmidarray addObject:dic];
                    if(index == 0)
                    {
                        NSString *mid = [dic objectForKey:@"mid"];
                        [AppData setpersonshopmid:mid];
                    }
                }
                
            }
            else
            {
                [self showTipInfo:response.msg];
            }
            
        }
        
    } onError:^(NSError *error){
        [self showTipInfo:@"网络异常，请重试！"];
        [self closeLoadView];
    }];
    
}


-(void)receivingAddress:(NSString *)userId
{
    [self showLoadViewExt:CGRectMake(0, 0, ScreenWidth, ScreenHeight-TopAndSystemHeight) backColor:[UIColor clearColor] info:@"正在加载中，请稍候..."];
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithObjectsAndKeys:userId,@"user_id", nil];
    [[HttpBaseModel getInstance] getDataResponsePath:@"user/getUserAddr" params:param httpMethod:@"POST" onCompletion:^(NSData *responseData){
        
        [self closeLoadView];
        NSError *error = nil;
        id resultString = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
        if (error)
        {
            NSLog(@"dic->%@",error);
        }
        else
        {
            ResponseCell *response = [[ResponseCell alloc] initWithDictionary:resultString error:NULL];
            if(response.success == 1)
            {
                NSArray *data = [resultString objectForKey:@"data"];
                for(int index = 0;index<[data count];index++)
                {
                    NSDictionary *dic = [data objectAtIndex:index];
                    AddressCell *cell = [[AddressCell alloc] initWithDictionary:dic error:NULL];
                    if([cell.is_default isEqualToString:@"1"])
                    {
                        [AppData shareInstance].addressCell = cell;
                        return ;
                    }
                }
            }
            else
            {
                [self showTipInfo:response.msg];
            }
        }
    } onError:^(NSError *error){
        [self showTipInfo:@"网络异常，请重试！"];
        [self closeLoadView];
    }];
}

@end
